package com.unohacha.qqj.fzdr.main;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.mylibrary.AppManager;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 二维码扫描
 */
public class CustomScanActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    @BindView(R.id.btn_switch)
    Button swichLight;
    @BindView(R.id.btn_hint1)
    Button hint2Show;
    @BindView(R.id.btn_hint2)
    Button btnHint2;
    @BindView(R.id.dbv_custom)
    DecoratedBarcodeView dbvCustom;

    private CaptureManager captureManager;
    private boolean isLightOn = false;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_scan);
        ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        commonTitle.setText("二维码扫描");
        commonRightImage.setVisibility(View.VISIBLE);
        commonRightImage.setImageResource(R.drawable.ic_light2);
        commonRightText.setVisibility(View.GONE);
        // 如果没有闪光灯功能，就去掉相关按钮
        if(!hasFlash()) {
            swichLight.setVisibility(View.GONE);
        }
        
        //重要代码，初始化捕获
        captureManager = new CaptureManager(this,dbvCustom);
        captureManager.initializeFromIntent(getIntent(),savedInstanceState);
        captureManager.decode();
        dbvCustom.setTorchListener(this);
    }

    
    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState);
        captureManager.onSaveInstanceState(outState);
    }

    // torch 手电筒
    @Override
    public void onTorchOn() {
        Toast.makeText(this,"开灯",Toast.LENGTH_LONG).show();
        isLightOn = true;
    }

    @Override
    public void onTorchOff() {
        Toast.makeText(this,"关灯",Toast.LENGTH_LONG).show();
        isLightOn = false;
    }


    @OnClick(R.id.common_right_image)
    public void setCommonRightImage(){
        if (isLightOn){
            dbvCustom.setTorchOff();
        }else {
            dbvCustom.setTorchOn();
        }
    }
    
    // 判断是否有闪光灯功能
    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @OnClick(R.id.common_back)
    public void setCommonBack(){
        finish();
    }
    
    @OnClick(R.id.btn_switch)
    public void setSwichLight(){
        if (isLightOn){
            dbvCustom.setTorchOff();
        }else {
            dbvCustom.setTorchOn();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dbvCustom.onKeyDown(keyCode,event) || super.onKeyDown(keyCode,event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }
}
